package com.yanggu.sa_token_sso_client.controller;

import cn.dev33.satoken.config.SaSsoConfig;
import cn.dev33.satoken.sso.SaSsoConsts;
import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.sso.SaSsoProcessor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Sa-Token-SSO Client端 Controller
 */
@RestController
public class SsoClientController {

    // 首页 
    @RequestMapping("/")
    public String index() {
        return "<h2>Sa-Token SSO-Client 应用端</h2>" +
                "<p>当前会话是否登录：" + StpUtil.isLogin() + "</p>" +
                "<p><a href=\"javascript:location.href='/sso/login?back=' + encodeURIComponent(location.href);\">登录</a> " +
                "<a href='/sso/logout?back=self'>注销</a></p>";
    }

    /**
     * Client端登录地址，接受参数：back=登录后的跳转地址
     */
    @RequestMapping("/sso/login")
    public Object ssoLogin() {
        return SaSsoProcessor.instance.ssoLogin();
    }

    /**
     * Client端单点注销地址（isSlo=true时打开），接受参数：back=注销后的跳转地址
     */
    @RequestMapping("/sso/logout")
    public Object ssoLogout() {
        return SaSsoProcessor.instance.ssoLogout();
    }

    /**
     * Client端单点注销回调地址（isSlo=true时打开），此接口为框架回调，开发者无需关心
     */
    @RequestMapping("/sso/logoutCall")
    public Object ssoLogoutCall() {
        SaSsoConfig cfg = SaSsoManager.getConfig();
        if (cfg.getIsSlo() && cfg.getIsHttp()) {
            return SaSsoProcessor.instance.ssoLogoutCall();
        }
        // 默认返回
        return SaSsoConsts.NOT_HANDLE;
    }

}
